﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;


namespace FXnaAppBase
{


    /// <summary>
    /// 关于信息类型的枚举值
    /// </summary>
    public enum MessageType
    {
        ERROR,
        WARNING,
        NORAML,
        SELFDEF
    }




    public  class DosCommand
    {


        public DosCommand()
        {


            this. _process = new Process();
 

        }







        private Process _process;

        public Process Process
        {
            get { return _process; }
          
        }




        private bool _noCreateWindow = true;


        /// <summary>
        /// 是否弹出黑框，true，不弹出
        /// </summary>
        public bool NoCreateWindow
        {
            get { return _noCreateWindow; }
            set { _noCreateWindow = value; }
        }


  



        /// <summary>
        /// 这个函数是为了测试一些信息
        /// </summary>
        public bool RunExeCommand(string exeName, string args, string workPath)
        {


       


         
            //输入对应的参数




            _process.StartInfo.FileName = workPath + exeName;
            _process.StartInfo.UseShellExecute = false;

            _process.StartInfo.CreateNoWindow = _noCreateWindow;
            _process.StartInfo.Arguments = args;


            _process.StartInfo.WorkingDirectory = workPath;




            if (_noCreateWindow)
            {

                _process.StartInfo.RedirectStandardError = true;
                _process.StartInfo.RedirectStandardInput = true;
                _process.StartInfo.RedirectStandardOutput = true;



            }

            else
            {


                _process.StartInfo.RedirectStandardInput = false;
                _process.StartInfo.RedirectStandardOutput = false;

                _process.StartInfo.RedirectStandardError = false;


            }














            try
            {

                _process.Start();





                //测试通读读取方法


                if (_noCreateWindow)
                {


                    _process.BeginOutputReadLine();

                    _process.BeginErrorReadLine();

                }




                //等待后处理运行结束,以前少这一句，杯具，哈哈


                _process.WaitForExit();




            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);

                return false;


            }

            finally
            {
                _process.Close();



            }


            return true;











        }


        protected void WriteMessage(string message, MessageType type)
        {
            //if (this._display != null)
            //{
            //    _display.Progress(message, type);

            //}
        }


     

        void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.Data))
            {




                this.WriteMessage(e.Data, MessageType.NORAML);




            }


        }

        protected void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {



          

            if (!string.IsNullOrEmpty(e.Data))
            {

                this.WriteMessage(e.Data, MessageType.ERROR);




            }





        }










        public void StopProcess()
        {
            if (_process != null)
            {
                try
                {

                    if (!_process.HasExited)
                    {


                        

                        _process.Kill();
                        _process.Close();


                        //this._display.Progress("计算被取消...", MessageType.SELFDEF);



                    }
                }
                catch { }

            }
        }


    }
}
